package com.naton.common.viewmodel

import androidx.lifecycle.*
import com.jeremyliao.liveeventbus.LiveEventBus
import com.naton.common.constant.CommConstant
import com.naton.common.ui.ViewBehavior

/**
 * 带有生命周期的ViewModel基类
 */
abstract class BaseViewModel : ViewModel() , ViewModelLifecycle , ViewBehavior {

    open var mToastMsg = MutableLiveData<String>()
    var mShowProgress = MutableLiveData<Boolean>()

    override fun showToast(msg: String) {
        mToastMsg.postValue(msg)
    }

    override fun showProgress() {
        mShowProgress.postValue(true)
    }

    override fun hideProgress() {
        mShowProgress.postValue(false)
    }

    override fun onCreate(owner: LifecycleOwner) {
        //监听指定吐司
        LiveEventBus.get(CommConstant.Live_Event_Bus_Show_Toast , String::class.java).observe(owner ,
            { t -> mToastMsg.value = t })
        //监听加载框状态
        LiveEventBus.get(CommConstant.Live_Event_Bus_Show_Progress , Boolean::class.java).observe(owner ,
            { t -> mShowProgress.value = t })
    }

    override fun onStart() {
    }

    override fun onResume() {
    }

    override fun onPause() {
    }

    override fun onStop() {
    }

    override fun onAny(owner: LifecycleOwner, event: Lifecycle.Event) {
    }

    override fun onDestroy() {
        //取消网络访问
    }
}